using System;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
using EnvDTE;
using EnvDTE80;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
//using VSLangProj2;
//using VSLangProj80;

namespace ProjectBase.Guidance.Actions
{
    /// <summary>
    /// Adds a solution folder item.
    /// </summary>
    [ServiceDependency(typeof (DTE))]
    public class MoveProjectToSolutionFolderAction : ConfigurableAction
    {
        //<Action Name="MoveCommonProjectToSolutionFolder"
        //           Type="ProjectBase.Guidance.Actions.MoveProjectToSolutionFolderAction, ProjectBase.Guidance"
        //           ProjectName ="Common"
        //           DestinationFolder="1 Common">

        private string destinationFolder;
        private ExpressionEvaluationService expressionEvaluationService;
        private string projectName;
        private IDictionaryService serviceToAdapt;
        private IUIService uiService;
        private DTE vs;

        [Input]
        public string ProjectName
        {
            get { return projectName; }
            set
            {
                projectName =
                    ExpressionEvaluationService.Evaluate(value, new ServiceAdapterDictionary(ServiceToAdapt)).ToString();
            }
        }

        [Input]
        public Project Project { get; set; }

        [Input]
        public string DestinationFolder
        {
            get { return destinationFolder; }
            set
            {
                destinationFolder =
                    ExpressionEvaluationService.Evaluate(value, new ServiceAdapterDictionary(ServiceToAdapt)).ToString();
            }
        }

        protected ExpressionEvaluationService ExpressionEvaluationService
        {
            get
            {
                if (expressionEvaluationService == null)
                    expressionEvaluationService = new ExpressionEvaluationService();

                return expressionEvaluationService;
            }
        }

        protected IDictionaryService ServiceToAdapt
        {
            get
            {
                if (serviceToAdapt == null)
                    serviceToAdapt = (IDictionaryService) ServiceHelper.GetService(this, typeof (IDictionaryService));

                return serviceToAdapt;
            }
        }

        public override void Execute()
        {
            if (string.IsNullOrEmpty(DestinationFolder))
                return;

            vs = GetService<DTE>();
            uiService = (IUIService) GetService(typeof (IUIService));

            try
            {
                Project project = Project ?? DteHelper.FindProjectByName(vs, ProjectName);

                SolutionFolder folder = DteHelper.FindSolutionFolderByPath(vs.Solution, DestinationFolder);
                if (project != null && folder != null)
                {
                    DteHelper.SelectItem(vs, project);
                    vs.ExecuteCommand("Edit.Cut", "");
                    DteHelper.SelectItem(vs, folder.Parent);
                    vs.ExecuteCommand("Edit.Paste", "");
                }
                else
                {
                    uiService.ShowMessage(string.Format("Unable to move the project {0} into solution folder {1}",
                                                        ProjectName, DestinationFolder));
                }
            }
            catch (Exception)
            {
                uiService.ShowMessage(string.Format("Unable to move the project {0} into solution folder {1}",
                                                    ProjectName, DestinationFolder));
                throw;
            }
        }

        public override void Undo()
        {
            // nothing to do.
        }
    }
}